package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.Rent;
import com.hzdl.car.bean.RentExcel;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.service.RentService;
import com.hzdl.car.util.*;
import com.hzdl.car.vo.CustomerVo;
import com.hzdl.car.vo.RentVo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("rent")
public class RentController {
    @Resource
    private RentService rentService;
    @Resource
    private CustomerService customerServicer;
    @RequestMapping("findCustomerById")
    public HzdlResultJson findCustomerById(@RequestParam(value = "identity") String id){
        try {
            List<Customer> list=customerServicer.findCustomerById(id);
            if(list.size()==0){
                return HzdlResultJson.error(2,"客户身份不存在，请重新输入");
            }else {

                return HzdlResultJson.ok(null);
            }
        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    @RequestMapping("getRentNo")
    public HzdlResultJson getRentNo(HttpSession session){
        try{
            String name = "";
            Object o =  session.getAttribute("user");
            if(o != null){
                User user = (User)o;
                name = user.getRealName();
            }
            Map<String ,Object> map = new HashMap<>();
            map.put("rentId", Common.createRandomStringUseTime("ZC"));
            map.put("openName",name);
            return HzdlResultJson.ok(map);
        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    @RequestMapping("addRent")
    public ResultObj addRent(RentVo rentVo){
        try{
            rentVo.setRentFlag(0);
            rentService.addRent(rentVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("findRent")
    public HzdlResultJson findRent(RentVo rentVo){
        PageInfo<Rent> pageInfo=rentService.findRent(rentVo);
        return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("updateRent")
    public ResultObj updateRent(RentVo rentVo){
        try {
            rentService.updateRent(rentVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }
    @RequestMapping("delRent")
    public ResultObj delRent(RentVo rentVo){
        try {
            rentService.delRent(rentVo);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("rentExcel")
    public ResponseEntity<Object> rentExcel(String rentId) {
        String fileName = "出租单打印.xls";
        try {

            RentExcel rentExcel=rentService.findRentExcel(rentId);

            // 通过工具类获取workbook对象
            ByteArrayOutputStream bos = RentExcelUtil.createExcelByRent(rentExcel,"czd-templet.xls");
            //处理文件名乱码
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //创建 封装响应头信息的对象
            HttpHeaders headers = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件的名称
            headers.setContentDispositionFormData("attachment", fileName);
            return new ResponseEntity<Object>(bos.toByteArray(), headers, HttpStatus.CREATED);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

}
